package com.zxl.grocery.modular.crawler.controller;

import com.zxl.grocery.common.annotion.Permission;
import com.zxl.grocery.common.annotion.log.BussinessLog;
import com.zxl.grocery.common.constant.Const;
import com.zxl.grocery.common.constant.Dict;
import com.zxl.grocery.common.controller.BaseController;
import com.zxl.grocery.common.exception.BizExceptionEnum;
import com.zxl.grocery.common.exception.BussinessException;
import com.zxl.grocery.common.persistence.dao.CrawlerBeautifulMapper;
import com.zxl.grocery.common.persistence.model.CrawlerBeautiful;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.modular.crawler.dao.BeautifulMgrDao;
import com.zxl.grocery.modular.crawler.warpper.BeautifulMgrWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 美图管理控制器
 *
 * @author zxl
 * @Date 2017-08-06 14:11:55
 */
@Controller
@RequestMapping("/beautifulMgr")
public class BeautifulMgrController extends BaseController {

    private String PREFIX = "/crawler/beautifulMgr/";

    @Resource
    private BeautifulMgrDao beautifulMgrDao;

    @Resource
    private CrawlerBeautifulMapper crawlerBeautifulMapper;

    /**
     * 跳转到美图管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "beautifulMgr.html";
    }

    /**
     * 跳转到添加美图管理
     */
    @RequestMapping("/beautifulMgr_add")
    public String beautifulMgrAdd() {
        return PREFIX + "beautifulMgr_add.html";
    }

    /**
     * 跳转到修改美图管理
     */
    @RequestMapping("/beautifulMgr_update/{beautifulMgrId}")
    public String beautifulMgrUpdate(@PathVariable Integer beautifulMgrId, Model model) {
        if (ToolUtil.isEmpty(beautifulMgrId)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CrawlerBeautiful crawlerBeautiful = crawlerBeautifulMapper.selectById(beautifulMgrId);
        model.addAttribute("crawlerBeautiful",crawlerBeautiful);
        LogObjectHolder.me().set(crawlerBeautiful);
        return PREFIX + "beautifulMgr_edit.html";
    }

    /**
     * 获取美图管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String ,Object>> result = beautifulMgrDao.getAllBeautifulList();
        return super.warpObject(new BeautifulMgrWarpper(result));
    }

    /**
     * 新增美图管理
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @Permission(Const.ADMIN_NAME)
    @BussinessLog(value = "新增美图", key = "title,link", dict = Dict.BeautifulDict)
    @ResponseBody
    public Object add(CrawlerBeautiful crawlerBeautiful) {
        crawlerBeautiful.setCreateDate(new Date());
        crawlerBeautiful.setAuthor(ShiroKit.getUser().getName());
        //System.out.println(crawlerBeautiful.toString());
        crawlerBeautiful.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除美图管理
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除美图", key = "title,beautifulMgrId", dict = Dict.DeleteDict)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer beautifulMgrId) {
        if (ToolUtil.isEmpty(beautifulMgrId)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.crawlerBeautifulMapper.deleteById(beautifulMgrId);
        return SUCCESS_TIP;
    }


    /**
     * 修改美图管理
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改美图", key = "title,,link,typeId,sourcesId,placedTop,status,sort,label,recommend,albumid", dict = Dict.BeautifulDict)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(CrawlerBeautiful crawlerBeautiful) {
        if (ToolUtil.isEmpty(crawlerBeautiful) || crawlerBeautiful.getId() == null){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        int id = crawlerBeautiful.getId();
        CrawlerBeautiful temp = crawlerBeautifulMapper.selectById(id);
        crawlerBeautiful.setAuthor(ShiroKit.getUser().getName());
        crawlerBeautiful.setId(temp.getId());
        crawlerBeautiful.setModifyDate(new Date());
        crawlerBeautifulMapper.updateById(crawlerBeautiful);
        return super.SUCCESS_TIP;
    }

    /**
     * 美图管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
